const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const appointTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [hour, minute].map(formatNumber).join(':')
}

const formatDateTo = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('-');
}

const formatNewDateTo = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month].map(formatNumber).join('-');
}

const formatNextDateTo = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 2
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month].map(formatNumber).join('-');
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const siet = function(lat, lon, nameText, addText){   //地图
  console.log(lat, lon, nameText, addText)
  wx.openLocation({
    latitude: lat,
    longitude: lon ,
    name: nameText,
    address: addText,
    scale: 28
  })
}
const phone = function(data,venue){ 
  // data 状态  venue 场馆 课程获取的电话
  //电话 1 客服电话  2 	合作加盟电话	   3场馆电话  （1 2电话是死值）
  var Iphone;
  switch (Number(data)) {
    case 1:
      Iphone = '4006199559';
      break;
      case 2:
      Iphone = venue;
      break;
    case 3:
      Iphone = venue;
  }
  wx.makePhoneCall({
    phoneNumber: Iphone //仅为示例，并非真实的电话号码
  })
}

function json2Form(json) {
  var str = [];
  for (var p in json) {
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
  }
  return str.join("&");
}  

function todate(inputstr, showsplit, showweek) {
  //Wed Mar 22 13:38:37 CST 2017
  inputstr = inputstr + ""; //末尾加一个空格
  var date = "";
  var month = new Array();
  var week = new Array();
  var str = ""

  month["Jan"] = 1; month["Feb"] = 2; month["Mar"] = 3; month["Apr"] = 4; month["May"] = 5; month["Jan"] = 6;
  month["Jul"] = 7; month["Aug"] = 8; month["Sep"] = 9; month["Oct"] = 10; month["Nov"] = 11; month["Dec"] = 12;
  week["Mon"] = "一"; week["Tue"] = "二"; week["Wed"] = "三"; week["Thu"] = "四"; week["Fri"] = "五"; week["Sat"] = "六"; week["Sun"] = "日";

  str = inputstr.split(" ");

  date = str[5];
  date += showsplit + month[str[1]] + showsplit + str[2];
  if (showweek) {
    date += "    " + " 星期" + week[str[0]];
  }

  return date;
}


module.exports = {
  formatTime: formatTime,
  siet: siet,
  phone: phone,
  json2Form: json2Form,
  appointTime: appointTime,
  formatDateTo: formatDateTo,
  formatNewDateTo: formatNewDateTo,
  formatNextDateTo: formatNextDateTo,
  todate: todate
}